<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html 
     PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <title>Class: ActionController::TestCase</title>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  <meta http-equiv="Content-Script-Type" content="text/javascript" />
  <link rel="stylesheet" href="../.././rdoc-style.css" type="text/css" media="screen" />
  <script type="text/javascript">
  // <![CDATA[

  function popupCode( url ) {
    window.open(url, "Code", "resizable=yes,scrollbars=yes,toolbar=no,status=no,height=150,width=400")
  }

  function toggleCode( id ) {
    if ( document.getElementById )
      elem = document.getElementById( id );
    else if ( document.all )
      elem = eval( "document.all." + id );
    else
      return false;

    elemStyle = elem.style;
    
    if ( elemStyle.display != "block" ) {
      elemStyle.display = "block"
    } else {
      elemStyle.display = "none"
    }

    return true;
  }
  
  // Make codeblocks hidden by default
  document.writeln( "<style type=\"text/css\">div.method-source-code { display: none }</style>" )
  
  // ]]>
  </script>

</head>
<body>



    <div id="classHeader">
        <table class="header-table">
        <tr class="top-aligned-row">
          <td><strong>Class</strong></td>
          <td class="class-name-in-header">ActionController::TestCase</td>
        </tr>
        <tr class="top-aligned-row">
            <td><strong>In:</strong></td>
            <td>
                <a href="../../files/vendor/rails/actionpack/lib/action_controller/test_case_rb.html">
                vendor/rails/actionpack/lib/action_controller/test_case.rb
                </a>
        <br />
            </td>
        </tr>

        <tr class="top-aligned-row">
            <td><strong>Parent:</strong></td>
            <td>
                <a href="../ActiveSupport/TestCase.html">
                ActiveSupport::TestCase
               </a>
            </td>
        </tr>
        </table>
    </div>
  <!-- banner header -->

  <div id="bodyContent">



  <div id="contextContent">

    <div id="description">
      <p>
Superclass for ActionController functional <a
href="TestCase.html#M001011">tests</a>. Functional <a
href="TestCase.html#M001011">tests</a> allow you to test a single
controller action per test method. This should not be confused with
integration <a href="TestCase.html#M001011">tests</a> (see <a
href="IntegrationTest.html">ActionController::IntegrationTest</a>), which
are more like &quot;stories&quot; that can involve multiple controllers and
mutliple actions (i.e. multiple different HTTP requests).
</p>
<h2>Basic example</h2>
<p>
Functional <a href="TestCase.html#M001011">tests</a> are written as
follows:
</p>
<ol>
<li>First, one uses the <tt>get</tt>, <tt>post</tt>, <tt>put</tt>,
<tt>delete</tt> or <tt>head</tt> method to simulate an HTTP request.

</li>
<li>Then, one asserts whether the current state is as expected.
&quot;State&quot; can be anything: the controller&#8216;s HTTP response,
the database contents, etc.

</li>
</ol>
<p>
For example:
</p>
<pre>
  class BooksControllerTest &lt; ActionController::TestCase
    def test_create
      # Simulate a POST response with the given HTTP parameters.
      post(:create, :book =&gt; { :title =&gt; &quot;Love Hina&quot; })

      # Assert that the controller tried to redirect us to
      # the created book's URI.
      assert_response :found

      # Assert that the controller really put the book in the database.
      assert_not_nil Book.find_by_title(&quot;Love Hina&quot;)
    end
  end
</pre>
<h2>Special instance variables</h2>
<p>
<a href="TestCase.html">ActionController::TestCase</a> will also
automatically provide the following instance variables for use in the <a
href="TestCase.html#M001011">tests</a>:
</p>
<table>
<tr><td valign="top"><b>@controller</b>:</td><td>The controller instance that will be tested.

</td></tr>
<tr><td valign="top"><b>@request</b>:</td><td>An ActionController::TestRequest, representing the current HTTP request.
You can modify this object before sending the HTTP request. For example,
you might want to set some session properties before sending a GET request.

</td></tr>
<tr><td valign="top"><b>@response</b>:</td><td>An <a href="TestResponse.html">ActionController::TestResponse</a> object,
representing the response of the last HTTP response. In the above example,
<tt>@response</tt> becomes valid after calling <tt>post</tt>. If the
various assert methods are not sufficient, then you may use this object to
inspect the HTTP response in detail.

</td></tr>
</table>
<p>
(Earlier versions of <a href="../Rails.html">Rails</a> required each
functional test to subclass Test::Unit::TestCase and define @controller,
@request, @response in <tt>setup</tt>.)
</p>
<h2>Controller is automatically inferred</h2>
<p>
<a href="TestCase.html">ActionController::TestCase</a> will automatically
infer the controller under test from the test class name. If the controller
cannot be inferred from the test class name, you can explicity set it with
<tt><a href="TestCase.html#M001011">tests</a></tt>.
</p>
<pre>
  class SpecialEdgeCaseWidgetsControllerTest &lt; ActionController::TestCase
    tests WidgetController
  end
</pre>

    </div>


   </div>

    <div id="method-list">
      <h3 class="section-bar">Methods</h3>

      <div class="name-list">
      <a href="#M001013">controller_class</a>&nbsp;&nbsp;
      <a href="#M001012">controller_class=</a>&nbsp;&nbsp;
      <a href="#M001014">determine_default_controller_class</a>&nbsp;&nbsp;
      <a href="#M001015">prepare_controller_class</a>&nbsp;&nbsp;
      <a href="#M001017">rescue_action_in_public!</a>&nbsp;&nbsp;
      <a href="#M001016">setup_controller_request_and_response</a>&nbsp;&nbsp;
      <a href="#M001011">tests</a>&nbsp;&nbsp;
      </div>
    </div>

  </div>


    <!-- if includes -->

    <div id="section">

    <div id="class-list">
      <h3 class="section-bar">Classes and Modules</h3>

      Module <a href="TestCase/RaiseActionExceptions.html" class="link">ActionController::TestCase::RaiseActionExceptions</a><br />

    </div>




      


    <!-- if method_list -->
    <div id="methods">
      <h3 class="section-bar">Public Class methods</h3>

      <div id="method-M001013" class="method-detail">
        <a name="M001013"></a>

        <div class="method-heading">
          <a href="#M001013" class="method-signature">
          <span class="method-name">controller_class</span><span class="method-args">()</span>
          </a>
        </div>
      
        <div class="method-description">
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M001013-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M001013-source">
<pre>
     <span class="ruby-comment cmt"># File vendor/rails/actionpack/lib/action_controller/test_case.rb, line 114</span>
114:       <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">controller_class</span>
115:         <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">current_controller_class</span> = <span class="ruby-identifier">read_inheritable_attribute</span>(<span class="ruby-identifier">:controller_class</span>)
116:           <span class="ruby-identifier">current_controller_class</span>
117:         <span class="ruby-keyword kw">else</span>
118:           <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">controller_class</span> = <span class="ruby-identifier">determine_default_controller_class</span>(<span class="ruby-identifier">name</span>)
119:         <span class="ruby-keyword kw">end</span>
120:       <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M001012" class="method-detail">
        <a name="M001012"></a>

        <div class="method-heading">
          <a href="#M001012" class="method-signature">
          <span class="method-name">controller_class=</span><span class="method-args">(new_class)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M001012-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M001012-source">
<pre>
     <span class="ruby-comment cmt"># File vendor/rails/actionpack/lib/action_controller/test_case.rb, line 109</span>
109:       <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">controller_class=</span>(<span class="ruby-identifier">new_class</span>)
110:         <span class="ruby-identifier">prepare_controller_class</span>(<span class="ruby-identifier">new_class</span>)
111:         <span class="ruby-identifier">write_inheritable_attribute</span>(<span class="ruby-identifier">:controller_class</span>, <span class="ruby-identifier">new_class</span>)
112:       <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M001014" class="method-detail">
        <a name="M001014"></a>

        <div class="method-heading">
          <a href="#M001014" class="method-signature">
          <span class="method-name">determine_default_controller_class</span><span class="method-args">(name)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M001014-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M001014-source">
<pre>
     <span class="ruby-comment cmt"># File vendor/rails/actionpack/lib/action_controller/test_case.rb, line 122</span>
122:       <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">determine_default_controller_class</span>(<span class="ruby-identifier">name</span>)
123:         <span class="ruby-identifier">name</span>.<span class="ruby-identifier">sub</span>(<span class="ruby-regexp re">/Test$/</span>, <span class="ruby-value str">''</span>).<span class="ruby-identifier">constantize</span>
124:       <span class="ruby-keyword kw">rescue</span> <span class="ruby-constant">NameError</span>
125:         <span class="ruby-identifier">raise</span> <span class="ruby-constant">NonInferrableControllerError</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">name</span>)
126:       <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M001015" class="method-detail">
        <a name="M001015"></a>

        <div class="method-heading">
          <a href="#M001015" class="method-signature">
          <span class="method-name">prepare_controller_class</span><span class="method-args">(new_class)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M001015-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M001015-source">
<pre>
     <span class="ruby-comment cmt"># File vendor/rails/actionpack/lib/action_controller/test_case.rb, line 128</span>
128:       <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">prepare_controller_class</span>(<span class="ruby-identifier">new_class</span>)
129:         <span class="ruby-identifier">new_class</span>.<span class="ruby-identifier">send</span> <span class="ruby-identifier">:include</span>, <span class="ruby-constant">RaiseActionExceptions</span>
130:       <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M001011" class="method-detail">
        <a name="M001011"></a>

        <div class="method-heading">
          <a href="#M001011" class="method-signature">
          <span class="method-name">tests</span><span class="method-args">(controller_class)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Sets the controller class name. Useful if the name can&#8216;t be inferred
from test class. Expects <tt><a
href="TestCase.html#M001013">controller_class</a></tt> as a constant.
Example: <tt><a href="TestCase.html#M001011">tests</a>
WidgetController</tt>.
</p>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M001011-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M001011-source">
<pre>
     <span class="ruby-comment cmt"># File vendor/rails/actionpack/lib/action_controller/test_case.rb, line 105</span>
105:       <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">tests</span>(<span class="ruby-identifier">controller_class</span>)
106:         <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">controller_class</span> = <span class="ruby-identifier">controller_class</span>
107:       <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <h3 class="section-bar">Public Instance methods</h3>

      <div id="method-M001017" class="method-detail">
        <a name="M001017"></a>

        <div class="method-heading">
          <a href="#M001017" class="method-signature">
          <span class="method-name">rescue_action_in_public!</span><span class="method-args">()</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Cause the action to be rescued according to the regular rules for
rescue_action when the visitor is not local
</p>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M001017-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M001017-source">
<pre>
     <span class="ruby-comment cmt"># File vendor/rails/actionpack/lib/action_controller/test_case.rb, line 143</span>
143:     <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">rescue_action_in_public!</span>
144:       <span class="ruby-ivar">@request</span>.<span class="ruby-identifier">remote_addr</span> = <span class="ruby-value str">'208.77.188.166'</span> <span class="ruby-comment cmt"># example.com</span>
145:     <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M001016" class="method-detail">
        <a name="M001016"></a>

        <div class="method-heading">
          <a href="#M001016" class="method-signature">
          <span class="method-name">setup_controller_request_and_response</span><span class="method-args">()</span>
          </a>
        </div>
      
        <div class="method-description">
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M001016-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M001016-source">
<pre>
     <span class="ruby-comment cmt"># File vendor/rails/actionpack/lib/action_controller/test_case.rb, line 133</span>
133:     <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">setup_controller_request_and_response</span>
134:       <span class="ruby-ivar">@controller</span> = <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">class</span>.<span class="ruby-identifier">controller_class</span>.<span class="ruby-identifier">new</span>
135:       <span class="ruby-ivar">@controller</span>.<span class="ruby-identifier">request</span> = <span class="ruby-ivar">@request</span> = <span class="ruby-constant">TestRequest</span>.<span class="ruby-identifier">new</span>
136:       <span class="ruby-ivar">@response</span> = <span class="ruby-constant">TestResponse</span>.<span class="ruby-identifier">new</span>
137: 
138:       <span class="ruby-ivar">@controller</span>.<span class="ruby-identifier">params</span> = {}
139:       <span class="ruby-ivar">@controller</span>.<span class="ruby-identifier">send</span>(<span class="ruby-identifier">:initialize_current_url</span>)
140:     <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>


    </div>


  </div>


<div id="validator-badges">
  <p><small><a href="http://validator.w3.org/check/referer">[Validate]</a></small></p>
</div>

</body>
</html>